package com.atguigu.tingshu.handler;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class RedisDelayHandle {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;

    @PostConstruct
    public void listener() {
        //创建线程
        new Thread(()->{
            //一直监听
            while(true) {

                try {
                    RBlockingQueue<String> blockingQueue =
                            redissonClient.getBlockingQueue(MqConst.EXCHANGE_CANCEL_ORDER);

                    String id = blockingQueue.take();

                    if(StringUtils.hasText(id)) {
                        orderInfoService.orderCancel(Long.parseLong(id));
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
}
